import { defineStore } from "pinia";
import { getBalanceDetailsAPI } from "@/apis/wallet"; // 假设你有这个API

export const useWalletStore = defineStore("wallet", {
  state: () => ({
    balance: 0,
    transactions: [],
    loading: false,
  }),

  actions: {
    // 获取余额
    async getBalance() {
      // 实现获取余额的API调用
    },

    // 获取余额明细
    async getBalanceDetails(params) {
      this.loading = true;
      try {
        const res = await getBalanceDetailsAPI(params);
        if (res.code === "200") {
          if (params.page === 1) {
            this.transactions = res.data.list || [];
          } else {
            this.transactions = [
              ...this.transactions,
              ...(res.data.list || []),
            ];
          }
          return res.data;
        } else {
          uni.showToast({
            title: res.msg || "获取余额明细失败",
            icon: "none",
          });
          return null;
        }
      } catch (error) {
        console.error("获取余额明细错误:", error);
        uni.showToast({
          title: "网络错误，请稍后重试",
          icon: "none",
        });
        return null;
      } finally {
        this.loading = false;
      }
    },
  },
});
